package splug.cjk;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;

import splug.cjk.packet.BBSPacket;

public class BBSNotice extends ListActivity implements OnItemClickListener, OnClickListener{
	private ArrayList<Content> content;
	private ArrayList<Content> sub_content;
	private ContentAdapter m_adapter;
	private String id;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("공지사항");
		setContentView(R.layout.bbs_notice);
		content = new ArrayList<Content>();
		sub_content = new ArrayList<Content>();
		
		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");
		
		Button back = (Button) findViewById(R.id.btn_bbs_notice_back);
		
		//뒤로 버튼이 눌렸을 때 액티비티 종료
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		try {
			Socket socket = new Socket(LetsGoActivity.server_ip, 2007);
			
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			
			OutputStreamWriter osw = new OutputStreamWriter(os);
			InputStreamReader isr = new InputStreamReader(is);
			
			osw.write('4');
			osw.flush();
			
			int ack = isr.read();
			
			if(ack == '1'){
				BBSPacket packet = new BBSPacket(null, null, 0, "", "", "", "");
				ObjectInputStream input = new ObjectInputStream(is);
				packet = (BBSPacket) input.readObject();
				
				content = packet.getContent();
				sub_content = packet.getSubContent();
			}
			else{
				Toast.makeText(BBSNotice.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
			}
			
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		m_adapter = new ContentAdapter(this, R.layout.list_property, content);
		setListAdapter(m_adapter);

	}

	private class ContentAdapter extends ArrayAdapter<Content> {

		private ArrayList<splug.cjk.Content> items;

		public ContentAdapter(Context context, int textViewResourceId, ArrayList<Content> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.list_property, null);
			}
			Content p = items.get(position);
			if (p != null) {
				TextView tt = (TextView) v.findViewById(R.id.toptext);
				TextView bt = (TextView) v.findViewById(R.id.bottomtext);
				if (tt != null){
					tt.setText(p.getTitle());
				}
				if(bt != null){
					bt.setText("글쓴이: "+ p.getId());
				}
			}
			return v;
		}
	}
	@Override
	public void onClick(View v) {
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long a) {
		super.onListItemClick(l, v, position, a);
		
		Intent intent = new Intent(BBSNotice.this, BBSContent.class);
		Bundle b = new Bundle();
		b.putString("ID", id);
		b.putString("AUTHOR", content.get(position).getId());
		b.putString("CONTENT", sub_content.get(position).getTitle());
		b.putString("TIME", sub_content.get(position).getId());
		intent.putExtras(b);
		startActivity(intent);
	}
}